﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;

namespace SnmpForm.APConfig
{
    public partial class ucLanConfig : ucAPConfig
    {
        public ucLanConfig()
        {
            InitializeComponent();
        }
        public override void LoadAp()
        {
            bool bPrivilege = BusinessWrapper.User.Instance.Permision(
                BusinessWrapper.Types.Privilege.ManageAP.Lan.Save.permision);
            buttonSave.Visible = bPrivilege;

            Update();
        }

        public override bool Update(bool bView = true)
        {
            DbWrapper.DbDataTypes.Ap info = m_ap.GetInfo();
            if (bView)
            {
                switch (info.landhcp)
                {
                    case "enable":
                        radioButtonAuto.Checked = true;

                        ipAddressControlIp.Enabled = false;
                        ipAddressControlNetmask.Enabled = false;
                        ipAddressControlGateway.Enabled = false;
                        break;
                    case "disable":
                        radioButtonManual.Checked = true;
                        break;
                    default:
                        break;
                }

                labelMac.Text = info.mac;

                string strIp = info.ip;
                byte[] ip = IpByteByStr(strIp);
                if (ip != null)
                {
                    ipAddressControlIp.SetAddressBytes(ip);
                }

                string strNetmask = info.netmask;
                byte[] netmask = IpByteByStr(strNetmask);
                if (netmask != null)
                {
                    ipAddressControlNetmask.SetAddressBytes(netmask);
                }

                string strGateway = info.gateway;
                byte[] gateway = IpByteByStr(strGateway);
                if (gateway != null)
                {
                    ipAddressControlGateway.SetAddressBytes(gateway);
                }
            }
            else
            {
                if (radioButtonAuto.Checked)
                {
                    info.landhcp = "enable";
                }
                else if (radioButtonManual.Checked)
                {
                    info.landhcp = "disable";

                    byte[] ip = ipAddressControlIp.GetAddressBytes();
                    IPAddress addrIP = new IPAddress(ip);
                    info.ip = addrIP.ToString();

                    byte[] netmask = ipAddressControlNetmask.GetAddressBytes();
                    IPAddress addrNetmask = new IPAddress(netmask);
                    info.netmask = addrNetmask.ToString();

                    byte[] gateway = ipAddressControlGateway.GetAddressBytes();
                    IPAddress addrGateway = new IPAddress(gateway);
                    info.gateway = addrGateway.ToString();
                }
                else
                {
                }
                
            }
            return true;
        }

        public override bool SyncToApCommit()
        {
            bool bSuccess = false;
            DbWrapper.DbDataTypes.Ap info = m_ap.GetInfo();
            bSuccess = m_ap.SetLanDhcp(info.landhcp);
            if (info.landhcp.Equals("disable"))
            {
                bSuccess = m_ap.SetLanIp(info.ip);
                if (!bSuccess)
                {
                    return bSuccess;
                }
                bSuccess = m_ap.SetLanNetmask(info.netmask);

                bSuccess = m_ap.SetLanGateway(info.gateway);
            }
            return bSuccess;
        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            Update(false);
            if (SyncToAp())
            {
                if (SaveToDb())
                {
                    NoticeConfigFormClose();
                }
            }
        }

        private void radioButtonManual_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButtonManual.Checked)
            {
                ipAddressControlIp.Enabled = true;
                ipAddressControlNetmask.Enabled = true;
                ipAddressControlGateway.Enabled = true;
            }
            else
            {
                ipAddressControlIp.Enabled = false;
                ipAddressControlNetmask.Enabled = false;
                ipAddressControlGateway.Enabled = false;
            }
        }

        private void radioButtonAuto_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButtonAuto.Checked)
            {
                ipAddressControlIp.Enabled = false;
                ipAddressControlNetmask.Enabled = false;
                ipAddressControlGateway.Enabled = false;
            }
        }
    }
}
